package com.blog.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blog.constant.MsgConstant;
import com.blog.pojo.entity.PageBean;
import com.blog.pojo.entity.Travel;
import com.blog.result.Result;
import com.blog.service.TravelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/travel")
public class TravelController {

    @Autowired
    private TravelService travelService;

    /**
     *分页查询旅行图片
     * @param currentPage
     * @param pageSize
     * @param tag
     * @return
     */
    @GetMapping("/page")
    public Result<PageBean> getTravelPage(
            @RequestParam(defaultValue = "1") Integer currentPage,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String tag
    ){
        log.info("开始旅行记录分页查询：{}, {}, {}", currentPage, pageSize, tag);

        try {
            Page<Travel> page = travelService.getTravelPage(currentPage, pageSize, tag);
            return Result.success(new PageBean<>(page.getTotal(), page.getRecords()));
        } catch (Exception e) {
            log.error("分页查询旅行图片失败：", e);
            return Result.error(MsgConstant.TRAVEL_SELECT_ERROR);
        }

    }

    /**
     * 添加旅行图片
     * @param travel
     * @return
     */
    @PostMapping("/add")
    public Result addTravel(@RequestBody Travel travel){
        log.info("开始添加旅行图片：{}", travel);

        try {
            travelService.addTravel(travel);
            return Result.success(MsgConstant.SUCCESS_MSG);
        } catch (Exception e) {
            log.error("添加旅行图片失败:", e);
            return Result.error(MsgConstant.REVIEW_ADD_ERROR);
        }

    }

    /**
     * 删除单个旅行图片
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteTravel(@PathVariable Long id){
        log.info("开始删除单个旅行图片：{}", id);

        try {
            travelService.deleteTravel(id);
            return Result.success(MsgConstant.SUCCESS_MSG);
        } catch (Exception e) {
            log.error("删除单个旅行图片失败：", e);
            return Result.error(MsgConstant.REVIEW_DELETE_ERROR);
        }

    }

    /**
     * 批量删除旅行图片
     * @param ids
     * @return
     */
    @DeleteMapping("/deleteBatch")
    public Result deleteTravels (@RequestBody List<Long> ids){
        log.info("开始批量删除旅行图片：{}", ids);

        try {
            travelService.deleteTravels(ids);
            return Result.success(MsgConstant.SUCCESS_MSG);
        } catch (Exception e) {
            log.error("批量删除旅行图片失败：", e);
            return Result.error(MsgConstant.TRAVEL_DELETE_ERRPR);
        }

    }

}
